<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\order\Goods;
use app\common\model\order\goods\Log;
use app\common\model\order\Order;
use app\common\model\User;

/**
 * 快递管理
 *
 * @icon fa fa-circle-o
 */
class Financial extends Backend
{

    /**
     * Express模型对象
     * @var \app\common\model\Express
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\user\Info();
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function performance()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $filter = json_decode($this->request->get("filter", ''), true);
        $op = json_decode($this->request->get("op", '', 'trim'), true);
        if (isset($filter['time'])) {
            //自定义处理
            $time = $filter['time'];
            $date = explode(' - ', $time);
            $time1 = $date[0];
            $time2 = $date[1];
            //处理完可以去掉也可以覆盖重构
            unset($filter['time'], $op['time']);
        }
        $this->request->get(['filter' => json_encode($filter, true)]);
        $this->request->get(['op' => json_encode($op, true)]);
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->with(['user', 'firstcate', 'twocate', 'group', 'upgradeinfo', 'investmentinfo'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as $key => $v) {
            // $ids = User::where("FIND_IN_SET('" . $v->user_id . "',paren_ids)")->column('id');
            $order = new Order();
            if (isset($time1) && $time1 && isset($time2) && $time2) {
                $order->whereBetween('paytime', [strtotime($time1), strtotime($time2)]);
                $conditions['paytime'] = ['between', [strtotime($time1), strtotime($time2)]];
            }
            // $order_ids = $order->where('order_status', '>', '0')->whereIn('user_id', $ids)->column('id');
            //我的团队消费金额
            $v->team_consumption = Log::where('user_id', $v->user_id)->where('p_shareholder', '>', 3)->where(['first_cate' => $v->first_cate, 'two_cate' => $v->two_cate])->sum('money');
            //创投系列消费金额
            $v->series_consumption = Log::where(['first_cate' => $v->first_cate, 'two_cate' => $v->two_cate, 'p_investment' => 6, 'user_id' =>  $v->user_id])->sum('money');
            //我的个人消费金额
            $conditions['user_id'] = $v->user_id;
            $idss = Order::where('order_status', '>', '0')->where($conditions)->column('id');
            $v->performance = Goods::whereIn('order_id', $idss)->where(['first_cate' => $v->first_cate, 'two_cate' => $v->two_cate])->sum('pay_fee');
        }

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
}
